package leetcode.code0429;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.tree.Node;

//执行用时：3 ms, 在所有 Java 提交中击败了73.31%的用户
//
//内存消耗：39.5 MB, 在所有 Java 提交中击败了5.02%的用户
//
//通过测试用例：38 / 38
public class Solution implements IDebug {

	public List<List<Integer>> levelOrder(Node root) {
		List<List<Integer>> ans = new ArrayList<>();
		Queue<Node> queue = new LinkedList<>();
		queue.add(root);
		while (!queue.isEmpty()) {
			int size = queue.size();
			List<Integer> list = new ArrayList<>();
			while (size > 0) {
				Node node = queue.poll();
				if (node != null) {
					for (Node child : node.children) {
						queue.add(child);
					}
					list.add(node.val);
				}
				size--;
			}
			if (list.size() > 0)// 空root
				ans.add(list);
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
